package com.cool.controller.sysmanagecontroller;

import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.sysmanagepo.ClassifyPo;
import com.cool.service.sysmanage.ClassifyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类控制器
 *
 * @author Jiangmanman
 * @date 2020/07/29
 */
@RequestMapping("/resourceCategory")
@ResponseBody
@Controller
public class ClassifyController {

    /**
     * 分类服务
     *
     * @date 2020/07/30
     * @see ClassifyService
     */
    @Resource
    private ClassifyService classifyService;


    /**
     * 查询所有
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @RequestMapping("/listAll")
    public CommonResult queryAll(){

        List<ClassifyPo> list = classifyService.selectAll();

        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(list);
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id")Integer id){

        Integer len = classifyService.deleteById(id);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.CLASSIFY_DELETE_FAILURE).setMsg("更新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 创建
     *
     * @param classifyPo 阿宝的分类
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody ClassifyPo classifyPo ){
        Integer len = classifyService.insert(classifyPo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.CLASSIFY_INSERT_FAILURE).setMsg("插入失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 更新
     *
     * @param id         id
     * @param classifyPo 阿宝的分类
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id")Integer id, @RequestBody ClassifyPo classifyPo){
        Integer len = classifyService.update(classifyPo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.CLASSIFY_UPDATE_FAILURE).setMsg("新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }


}
